package com.design.fitment.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.design.fitment.R;
import com.design.fitment.adapter.CaseDetailListAdapter;
import com.design.fitment.app.MyApplication;
import com.design.fitment.db.MessageCollect;
import com.design.fitment.entity.CasesDtail;
import com.design.fitment.util.DesignNetworkUtil;
import com.design.fitment.util.RequestDataCallback;
import com.design.fitment.view.CasesDtailHeaderView;
import com.google.gson.Gson;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.List;

import cn.sharesdk.onekeyshare.OnekeyShare;

@ContentView(R.layout.activity_case)
public class CaseDetailActivity extends AppCompatActivity {
    private DbUtils dbUtils;
    private CaseDetailListAdapter mAdapter;
    @ViewInject(R.id.iv_cases_detail_collect)
    private RadioButton rbCollect;
    @ViewInject(R.id.lv_case)
    private ListView lv;
    private CasesDtail.DataEntity entity ;
    private CasesDtailHeaderView headerView;
    private List<CasesDtail.DataEntity.ImgListEntity> mList = new ArrayList<>();
    private int casesId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_case);
        ViewUtils.inject(this);
        Intent intent = getIntent();
        casesId = intent.getIntExtra("casesId",0);
        init();
        load();


    }

    private void load() {
        DesignNetworkUtil.getInstance().getCasesDetail(new RequestDataCallback() {
            @Override
            public void onSuccess(String response) {
                Gson gson = new Gson();
                CasesDtail detail = gson.fromJson(response, CasesDtail.class);
                if (detail != null) {
                    mList.addAll(detail.getData().getImgList());
                    entity = detail.getData();
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(HttpException error) {

            }
        }, casesId);
        headerView.loadBanner();
    }

    private void init() {
        headerView = new CasesDtailHeaderView(this,casesId);
        lv.addHeaderView(headerView);
        headerView.loadBanner();
        mAdapter = new CaseDetailListAdapter(this,mList);
        lv.setAdapter(mAdapter);
    }
    @OnClick(R.id.iv_cases_detail_fanhui)
    private void onClickBack(View view){

        this.finish();
        this.overridePendingTransition(R.anim.in_from_left, R.anim.out_from_right);
    }
    @OnClick(R.id.iv_cases_detail_share)
    private void onClickShare(View view){


            OnekeyShare oks = new OnekeyShare();
            //关闭sso授权
            oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
            //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
            // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
            oks.setTitle(entity.getName());
            // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
            oks.setTitleUrl(entity.getImgUrl());
            // text是分享文本，所有平台都需要这个字段
            oks.setText(entity.getComment());
            // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
            oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
            // url仅在微信（包括好友和朋友圈）中使用
            oks.setUrl(entity.getImgUrl());
            // comment是我对这条分享的评论，仅在人人网和QQ空间使用
            oks.setComment("我是测试评论文本");
            // site是分享此内容的网站名称，仅在QQ空间使用
            oks.setSite("设计本");
            // siteUrl是分享此内容的网站地址，仅在QQ空间使用
            oks.setSiteUrl(entity.getComment());

            // 启动分享GUI
            oks.show(this);

    }
    @OnClick(R.id.iv_cases_detail_collect)
    private void onClickCollect(View view){
        MessageCollect messageCollect = null;
        dbUtils = MyApplication.getApp().getDbUtils();
        try
        {
            messageCollect = dbUtils.findFirst(
                    Selector.from(
                            MessageCollect.class).where("message_id","=",casesId));

            // 如果收藏了，则取消收藏
            if (messageCollect != null)
            {
                // 删除收藏的内容
                dbUtils.delete(MessageCollect.class,
                        WhereBuilder.b("message_id", "=", casesId));
                rbCollect.setChecked(false);
                Toast.makeText(CaseDetailActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();

            }
            // 如果没有收藏，则收藏
            else if (messageCollect == null)
            {
                messageCollect = new MessageCollect();
                messageCollect.messageId = casesId+"";
                messageCollect.messageName = entity.getName();
                messageCollect.messageImageUrl = entity.getImgUrl();

                dbUtils.save(messageCollect);
                rbCollect.setChecked(true);
                Toast.makeText(CaseDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
            }
        }
        catch (DbException e)
        {
            e.printStackTrace();
        }
    }
}
